package com.pac.fragment;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.pac.API;
import com.pac.MyApp;
import com.pac.R;
import com.pac.bean.hot.Hot;
import com.pac.bean.hot.HotgcateDetails;
import com.pac.bean.hot.HotlistDetails;
import com.pac.utils.ImageUtils;

import java.util.List;

/**
 * 最热
 * Created by wang on 15-4-22.
 */
public class FMHot extends Fragment{
    private FMHotAdapter adapter;
    private PopupWindowListAdapter adapter2;
    private ListView listView;
    private Hot jsonStr;
    private DisplayMetrics dm;

    @Override
    public View onCreateView(LayoutInflater inflater,  ViewGroup container, Bundle savedInstanceState) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_hot, container, false);
        listView= (ListView) view.findViewById(R.id.hot_listview);
        MyApp.httpUtils.send(HttpRequest.HttpMethod.GET, API.HOT,new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> objectResponseInfo) {
                String result = objectResponseInfo.result;
                jsonStr = Hot.getInstanceFromJsonStr(result);
                adapter=new FMHotAdapter(jsonStr.getList());
                listView.setAdapter(adapter);
            }

            @Override
            public void onFailure(HttpException e, String s) {

            }
        });
        dm = getResources().getDisplayMetrics();
        Button button= (Button) view.findViewById(R.id.hot_button);
        button.setOnTouchListener(new View.OnTouchListener() {

            private int lastX;
            private int lastY;

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int ea=event.getAction();

                int screenWidth= dm.widthPixels;
                int screenHeight= dm.heightPixels-50;//需要减掉图片的高度
                switch(ea){
                    case MotionEvent.ACTION_DOWN:
                        lastX = (int) event.getRawX();
                        lastY = (int)event.getRawY();
                    case MotionEvent.ACTION_MOVE:
                        //event.getRawX();获得移动的位置
                        int dx=(int)event.getRawX()- lastX;
                        int dy=(int)event.getRawY()- lastY;
                        int l=v.getLeft()+dx;
                        int b=v.getBottom()+dy;
                        int r=v.getRight()+dx;
                        int t=v.getTop()+dy;

                        //下面判断移动是否超出屏幕
                        if(l<0){
                            l=0;
                            r=l+v.getWidth();
                        }
                        if(t<0){
                            t=0;
                            b=t+v.getHeight();
                        }
                        if(r>screenWidth){
                            r=screenWidth;
                            l=r-v.getWidth();
                        }
                        if(b>screenHeight){
                            b=screenHeight;
                            t=b-v.getHeight();
                        }
                        v.layout(l, t, r, b);
                        lastX =(int)event.getRawX();
                        lastY =(int)event.getRawY();
                        v.postInvalidate();
                        break;
                    case MotionEvent.ACTION_UP:
                        break;
                }

                return false;
            }
        });
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                PopupWindow popupWindow = new PopupWindow();
                popupWindow.setBackgroundDrawable(getResources().getDrawable(R.color.popwindow));
                popupWindow.setWidth(dm.widthPixels);
                popupWindow.setHeight(dm.heightPixels/5*3);
                popupWindow.setOutsideTouchable(true);
                adapter2=new PopupWindowListAdapter(jsonStr.getGcate());
                View window=LayoutInflater.from(getActivity()).inflate(R.layout.popupwindow,null);
                GridView windowGradview= (GridView) window.findViewById(R.id.popupwindow_gridview);
                windowGradview.setAdapter(adapter2);
                popupWindow.setContentView(window);
                popupWindow.showAtLocation(v,Gravity.CENTER,50,50);
            }
        });
        return view;
    }
    class FMHotAdapter extends BaseAdapter{
        private List<HotlistDetails> newsdata;

        FMHotAdapter(List<HotlistDetails> newsdata) {
            this.newsdata = newsdata;
        }

        @Override
        public int getCount() {
            return newsdata.size();
        }

        @Override
        public Object getItem(int position) {
            return newsdata.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView==null){
                holder=new ViewHolder();
                convertView=LayoutInflater.from(getActivity()).inflate(R.layout.fragment_hot_list,null);
                holder.comment = (TextView) convertView.findViewById(R.id.hot_list_comment);
                holder.content = (TextView) convertView.findViewById(R.id.hot_list_content);
                holder.user = (TextView) convertView.findViewById(R.id.hot_list_user);
                holder.face = (ImageView) convertView.findViewById(R.id.hot_list_face);
                holder.host = (ImageView) convertView.findViewById(R.id.hot_list_host);
                convertView.setTag(holder);
            }else {
                holder= (ViewHolder) convertView.getTag();
            }
            HotlistDetails data = newsdata.get(position);
            holder.user.setText(data.getUser_name());
            holder.content.setText(data.getContent());
            holder.comment.setText(data.getView()+"人气/"+data.getLaud()+"赞/"+data.getCollect()+"收藏/"+data.getComment()+"评论");
            String facepath=data.getFace_pic();
            final String hostpath=data.getHost_pic();
            final String url = data.getUrl();
            ImageUtils.loadImg(getActivity(),holder.face,facepath,true);
            ImageUtils.loadImg(getActivity(), holder.host, hostpath);
            holder.host.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Uri uri = Uri.parse(url);
                    Intent it = new Intent(Intent.ACTION_VIEW, uri);
                    startActivity(it);
                }
            });
            return convertView;
        }
        class ViewHolder{
            TextView comment;
            TextView content;
            TextView user;
            ImageView face;
            ImageView host;
        }
    }
    class PopupWindowListAdapter extends BaseAdapter{
        private List<HotgcateDetails> newsdata;

        PopupWindowListAdapter(List<HotgcateDetails> newsdata) {
            this.newsdata = newsdata;
        }

        @Override
        public int getCount() {
            return newsdata.size();
        }

        @Override
        public Object getItem(int position) {
            return newsdata.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = LayoutInflater.from(getActivity()).inflate(R.layout.popupwindow_list, null);
            ImageView image= (ImageView) convertView.findViewById(R.id.popupwindow_list_image);
            TextView text= (TextView) convertView.findViewById(R.id.popupwindow_list_text);

            HotgcateDetails news = newsdata.get(position);
            String ico = news.getIco();
            text.setText(news.getCate_name());
            ImageUtils.loadImg(getActivity(),image,ico,true);
            return convertView;
        }
    }

}

